﻿function insert() {
    $items = $(".doulist_item");
    $items.each(function(index){
    console.log("index=" + index)
        $link = $(this).find("a:first");
        console.log("link:" + $link.attr('href'));
        $subjectID = getIDFromURL($link.attr('href'));
        console.log("id:" + $subjectID);
        $.ajax({
            url : $link.attr('href'),
            type: "get",
            dataType: "html",
            success:function(result, textStatus){
                console.log("------------------------------------");
                console.log(result)
            }
        });
        
        /* $.ajax({
            url : "https://api.douban.com/v2/movie/" + $subjectID,
            type: "get",
            dataType: "json",
            success:function(result, textStatus){
                rating = result.rating.average;
                console.log("rating:" + rating);
                $p = $(this).find("p:first");
        
                $rating = $("<br><span>评分 : 1234</span></p>");
                $p.append($rating);
            }
        }); */
    });
}

function getIDFromURL(link) {
	//sample: http://movie.douban.com/subject/3016187/
	var id = link.split("/")[4];
	return id;
}

function getInfoViaAPIByID(subjectID) {

    
}

function sortMethod() {
    
}

function sortDoulist() {
    $div = $(".aside:first");
    $sortChoice = $('<p class="pl2">&gt; <a href="javascript:void(0)">将收藏的豆列排序[测试版]</a></p>');
    $div.append($sortChoice);
    $sortChoice.append("<br>");
    $sortChoice.append("<span class='pl'>目前在排序之后无法执行删除操作，需要重新刷新页面。</span>");
    
    $a = $sortChoice.find("a:first");
    $a.click(function(){
        
        $tbody = $(".list-b>tbody");
        $(".sortedRows").remove();
        //oldHtml = $tbody.html();
        //localeStorage["tbody"] = oldHtml;
        
        $rows = $(".list-b>tbody>tr");
        $newImg = $('<img src="http://img3.douban.com/pics/new_menu.gif" />');
        console.log("length:" + $rows.length);
        originalTimeArr = [];
        originalTdHtmlArr = [];
        $.each($rows, function(i, value){
            $(this).show();
            var text = $(this).find(".creator:first").text().trim();
            var length = text.length;
            var timeLength = 20; // sample: 2013-04-29 10:42:05
            var endingStringLen = 2; // all item ends with 创建
            var time = text.substring(length - timeLength - 1, length - 2);
            console.log(i + "time:" + time);
            originalTimeArr.push(time);
            originalTdHtmlArr.push($(this).html());
            newTimeArr = originalTimeArr.sort();
            newTimeArr = newTimeArr.reverse();
            $(this).hide();
        });
        console.log(originalTimeArr);
        rowLen = originalTdHtmlArr.length;
        
        $.each(newTimeArr, function(i){
            currentTimeStr = newTimeArr[i];
            console.log("currentTimeStr = " + currentTimeStr );
            for(var i = 0; i < rowLen; i++) {
                if(originalTdHtmlArr[i].indexOf(currentTimeStr) != -1) {
                    $newRow = $("<tr class='sortedRows'></tr>");
                    $td = $(originalTdHtmlArr[i]);
                    if(originalTdHtmlArr[i].indexOf(getTodayTimeStr()) != -1) {
                    console.log("add new image");
                        $div = $td.find(".creator:first");
                        $div.before('<img style="vertical-align:top;text-aligh:left;" src="http://img3.douban.com/pics/new_menu.gif">');
                        console.log($td.html());
                    } 
                    $newRow.append($td);
                    $tbody.append($newRow);
                    
                    break;
                }
            }
        });       
    });
    
}

function getTodayTimeStr() {
    var d = new Date();
    var vYear = d.getFullYear();
    var vMon = d.getMonth() + 1;
    if(vMon < 10) {
        vMon = "0" + vMon;
    }
    
    var vDay = d.getDate();
    if(vDay < 10) {
        vDay = "0" + vDay;
    }
    var h = d.getHours(); 
    var m = d.getMinutes(); 
    var se = d.getSeconds();

    // Sample: 2013-05-05
    return vYear + "-" + vMon + "-" + vDay;
}
sortDoulist();